<template>
  <div class="main">
    <div class="container">
      <div class="container__left">
        <div class="navbar">
          <span>{{ welcomestr }}</span>
        </div>
        <div>
          <div style="text-align: center; vertical-align: auto;">
            <img v-if="imgsrc" :src="imgsrc" width="800" height="550">
            <!-- <img v-else src="@/assets/background.png" width="800" height="550"> -->
            <h3>欢迎使用erp管理系统</h3>
          </div>
        </div>
      </div>
      <div class="container__right">
        <form class="content">
          <span></span>
          <div class="header">
          </div>
          <login-account @ok="handleSubmit" ref="alogin" @validateFail="validateFail" @success="requestSuccess"
            @fail="requestFailed"></login-account>
        </form>
      </div>
    </div>

    <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
      <!-- <login-account ref="alogin" @validateFail="validateFail" @success="requestSuccess"
        @fail="requestFailed"></login-account> -->

      <!-- <a-tabs :activeKey="customActiveKey" :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change="handleTabClick">
        <a-tab-pane key="tab1" tab="账号密码登录">
         
        </a-tab-pane>
   

        <a-tab-pane key="tab2" tab="手机号登录" v-if="1 == 0">
          <login-phone ref="plogin" @validateFail="validateFail" @success="requestSuccess"
            @fail="requestFailed"></login-phone>
        </a-tab-pane>
      </a-tabs> -->
      <!-- <login-account ref="alogin" @validateFail="validateFail" @success="requestSuccess" @fail="requestFailed">
      </login-account> -->

      <!-- <a-form-model-item>
        <a-checkbox @change="handleRememberMeChange" default-checked>自动登录</a-checkbox>
        <router-link :to="{ name: 'alteration'}" class="forge-password" style="float: right;">
          忘记密码
        </router-link>
        <router-link :to="{ name: 'register'}" class="forge-password" style="float: right;margin-right: 10px" >
          注册账户
        </router-link>
      </a-form-model-item> -->

      <!-- <a-form-item style="margin-top:24px">
        <a-button size="large" type="primary" htmlType="submit" class="login-button" :loading="loginBtn"
          @click.stop.prevent="handleSubmit" :disabled="loginBtn">登录</a-button>
      </a-form-item> -->

      <!-- <a-button size="large" type="primary" htmlType="submit" class="login-button" :loading="loginBtn"
          @click.stop.prevent="handleSubmit" :disabled="loginBtn">登录</a-button> -->
    </a-form-model>
    <!-- 
    <two-step-captcha v-if="requiredTwoStepCaptcha" :visible="stepCaptchaVisible" @success="stepCaptchaSuccess" @cancel="stepCaptchaCancel"></two-step-captcha>
    <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
    <third-login ref="thirdLogin"></third-login> -->
    <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, ENCRYPTED_STRING } from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import { getEncryptedString } from '@/utils/encryption/aesEncrypt'
import { timeFix } from '@/utils/util'
import { getAction, getFileAccessHttpUrl } from '@/api/manage'
import LoginAccount from './LoginAccount'
import LoginPhone from './LoginPhone'
import domAlign from 'dom-align'

export default {
  components: {
    LoginSelectTenant,
    TwoStepCaptcha,
    ThirdLogin,
    LoginAccount,
    LoginPhone
  },
  data () {
    return {
      welcomestr: "",
      imgsrc: "",
      customActiveKey: 'tab1',
      rememberMe: true,
      loginBtn: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      encryptedString: {
        key: '',
        iv: ''
      }
    }
  },
  async created () {
    Vue.ls.remove(ACCESS_TOKEN)
    this.getRouterData()
    this.rememberMe = true

    var sysdeprtinfo = await getAction('/sys/sysDepart/getcompanydepart')
    if (sysdeprtinfo.success) {
      this.welcomestr = sysdeprtinfo.result.welcomestr;
      this.imgsrc = getFileAccessHttpUrl(sysdeprtinfo.result.logoimg);
    }
  },
  methods: {
    handleTabClick (key) {
      this.customActiveKey = key
    },
    handleRememberMeChange (e) {
      this.rememberMe = e.target.checked
    },
    /**跳转到登录页面的参数-账号获取*/
    getRouterData () {
      this.$nextTick(() => {
        let temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },

    //登录
    handleSubmit () {
      this.loginBtn = true
      if (this.customActiveKey === 'tab1') {
        // 使用账户密码登录
        this.$refs.alogin.handleLogin(this.rememberMe)
      } else {
        //手机号码登录
        this.$refs.plogin.handleLogin(this.rememberMe)
      }
    },
    // 校验失败
    validateFail () {
      this.loginBtn = false
    },
    // 登录后台成功
    requestSuccess (loginResult) {
      this.$refs.loginSelect.show(loginResult)
    },
    //登录后台失败
    requestFailed (err) {
      let description = ((err.response || {}).data || {}).message || err.message || '请求出现错误，请稍后再试'
      this.$notification['error']({
        message: '登录失败',
        description: description,
        duration: 4
      })
      //账户密码登录错误后更新验证码
      if (this.customActiveKey === 'tab1' && description.indexOf('密码错误') > 0) {
        this.$refs.alogin.handleChangeCheckCode()
      }
      this.loginBtn = false
    },
    loginSelectOk () {
      this.loginSuccess()
    },
    //登录成功
    loginSuccess () {
      this.$router.push({ path: '/dashboard/analysis' }).catch(() => {
        console.log('登录跳转首页出错,这个错误从哪里来的')
      })
      this.$notification.success({
        message: '欢迎',
        description: `${timeFix()}，欢迎回来`
      })
    },

    stepCaptchaSuccess () {
      this.loginSuccess()
    },
    stepCaptchaCancel () {
      this.Logout().then(() => {
        this.loginBtn = false
        this.stepCaptchaVisible = false
      })
    },
    //获取密码加密规则
    getEncrypte () {
      var encryptedString = Vue.ls.get(ENCRYPTED_STRING)
      if (encryptedString == null) {
        getEncryptedString().then(data => {
          this.encryptedString = data
        })
      } else {
        this.encryptedString = encryptedString
      }
    }
  }
}
</script>
<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
<style>
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}

.container {
  display: flex;
  min-height: 100vh;
}

.container__left {
  flex: 2;
  /* background: linear-gradient(to bottom, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)); */
  background-color: rgb(59, 130, 246);
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
}

.navbar {
  padding: 1rem 2rem;
  color: var(--extra-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.navbar span {
  font-size: 1.5rem;
  font-weight: 500;
  cursor: pointer;
}

.image__content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--extra-light);
}

.image__content h2 {
  font-size: 3rem;
  margin-bottom: 30rem;
}

.image__content p {
  letter-spacing: 2px;
}

.image__subtitle {
  text-align: center;
}

.container__right {
  flex: 1.25;
  display: flex;
  align-items: center;
  justify-content: center;
}

.content {
  width: 100%;
  max-width: 550px;
  padding: 1rem;
  display: grid;
  grid-template-columns: 150px auto;
  gap: 1rem;
  color: var(--secondary-color);
}

.header {
  margin-bottom: 1rem;
}

.header h1 {
  font-size: 2.5rem;
  font-weight: 900;
  color: var(--primary-color);
}

.header p {
  letter-spacing: 2px;
}

.content span {
  display: grid;
  align-items: center;
  font-size: 1rem;
  font-weight: 600;
}

.content .gender {
  display: flex;
  gap: 1rem;
}

.content .gender span:nth-child(odd) {
  height: 40px;
  width: 40px;
  display: grid;
  place-content: center;
  border-radius: 100%;
  color: var(--extra-light);
  font-size: 1.25rem;
  font-weight: 400;
  cursor: pointer;
}

.content .gender span:nth-child(1) {
  background-color: var(--primary-color);
}

.content .gender span:nth-child(3) {
  background-color: var(--secondary-color);
}

.subscription {
  padding: 1rem;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.subscription span {
  transition: 0.3s;
  cursor: pointer;
}

.subscription span:hover {
  color: var(--primary-color);
}

input {
  width: 100%;
  padding: 1rem;
  background-color: var(--text-light);
  outline: none;
  border: none;
  border-radius: 5px;
}

.btns {
  display: flex;
  gap: 1rem;
}

.btn {
  flex: 1;
  padding: 1rem;
  outline: none;
  border: none;
  font-size: 1rem;
  color: var(--extra-light);
  cursor: pointer;
}

.cancel {
  background-color: var(--secondary-color);
}

.confirm {
  background-color: var(--primary-color);
}
</style>
